package logic

import (
	"context"
	"fmt"
	"tiktok/model/mysql"
	cache "tiktok/model/redis"

	"tiktok/tiktokrpc/internal/svc"
	"tiktok/tiktokrpc/tiktokrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *tiktokrpc.LoginRequest) (*tiktokrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.TikTokUser

	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("当前手机号未注册，请前去注册")
	}
	get := cache.Redis.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SmsCode {
		return nil, fmt.Errorf("验证码错误请重新输入")

	}

	return &tiktokrpc.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
